package com.yan.demo;

import java.util.Random;

public class MethodTest3 {
    public static void main(String[] args) {
        String code = verificationCode(5);
        System.out.println(code);
    }

    public static String verificationCode(int n){
        int i;
        Random r = new Random();
        // 定义字符串用于连接
        String str = "";
        int num;
        for (int j = 0; j < n; j++) {
            // 生成三个随机数 0-数字 1-小写字母 2-大写字母
            i = r.nextInt(3);
            switch(i){
                case 0 : // 生成0-9随机数字
                    num = r.nextInt(10);
                    str = str + num;
                    break;
                case 1 : // 生成26个小写字母
                    num = r.nextInt(26) + 97;
                    char xiaoZiMu = (char) num;
                    str = str + xiaoZiMu;
                    break;
                case 2 :
                    num = r.nextInt(26) + 65;
                    char daZiMu = (char) num;
                    str = str + daZiMu;
                    break;
            }
        }
        return str;
    }
}
